Page({
  data: {
    collegeList: [],
    majorList: {},
    currentCollegeId: null // 当前展开的学院ID
  },

  onLoad() {
    this.getCollegeList();
  },

  getCollegeList() {
    wx.request({
      url: 'http://localhost:8080/vocationalcollege/list', // 替换为实际的后端接口地址
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            collegeList: res.data.data
          });
          this.getMajorsForColleges(this.data.collegeList);
        }
      }
    });
  },

  getMajorsForColleges(colleges) {
    colleges.forEach(college => {
      wx.request({
        url: 'http://localhost:8080/vocationalmajor/listByCollegeId', // 替换为实际的后端接口地址
        method: 'GET',
        data: { collegeId: college.id },
        success: (res) => {
          if (res.statusCode === 200) {
            const majorList = this.data.majorList;
            majorList[college.id] = res.data.data;
            this.setData({
              majorList: majorList
            });
          }
        }
      });
    });
  },

  toggleMajorList(e) {
    const collegeId = e.currentTarget.dataset.id;
    this.setData({
      currentCollegeId: this.data.currentCollegeId === collegeId ? null : collegeId
    });
  }
});